package com.solution.admin.upload.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.solution.common.config.PublicConfig;
import com.solution.common.utils.DateUtils;
import com.solution.common.utils.upload.OssUploadFile;

/**
 * 上传文件控制层
 * @author yaoyu
 * @date 2017年4月15日 下午4:51:21
 */
@RequestMapping("/uploadFile")
@Controller
public class UploadFileController {
	
	private static final Logger log = LoggerFactory.getLogger(UploadFileController.class);
	
	/**
	 * 上传单个文件
	 * @param uploadFile
	 * @return
	 * @author yaoyu
	 * @date 2017年4月15日 下午5:43:00
	 */
	@RequestMapping(value="/uploadOneFile", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String uploadOneFile(HttpServletRequest request) {
		// 返回值
		JSONObject result = new JSONObject();
		// 获取文件
		MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
		Set<String> keySet = fileMap.keySet();
		Iterator<String> iterator = keySet.iterator();
		try {
			while (iterator.hasNext()) {
				String key = iterator.next();
				MultipartFile multipartFile = fileMap.get(key);
				// 源文件名
				String originalFilename = multipartFile.getOriginalFilename();
				// 后缀名
				String extName = FilenameUtils.getExtension(originalFilename);
				// 文件重新命名
				String newFileName = DateUtils.getDate("yyyyMMddHHmmssSSS") + "." + extName;
				// 上传
				InputStream is = multipartFile.getInputStream();
				String filePath = OssUploadFile.uploadFileToAliyun(is, newFileName, multipartFile.getSize());
				
				result.put("code", "1");
				result.put("filePath", filePath);
				result.put("msg", "上传文件成功");
			}
		} catch (Exception e) {
			result.put("code", "-1");
			result.put("msg", "上传文件失败");
			log.error("上传文件异常===", e);
		}
		return result.toJSONString();
	}
	
	/**
	 * 上传单个或多个文件（Ueditor）
	 * @param request
	 * @param response
	 * @author yaoyu
	 * @date 2017年4月21日 上午10:40:27
	 */
	@RequestMapping(value="/uploadMultiFile4Ueditor", produces="text/html;charset=UTF-8")
	public void uploadMultiFile4Ueditor(HttpServletRequest request, HttpServletResponse response) {
		
		MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
		Set<String> keySet = fileMap.keySet();
		Iterator<String> iterator = keySet.iterator();
		
		PrintWriter out = null;
		try {
			out = response.getWriter();
			while (iterator.hasNext()) {
				String key = iterator.next();
				MultipartFile multipartFile = fileMap.get(key);
				// 源文件名
				String originalFilename = multipartFile.getOriginalFilename();
				// 后缀名
				String extName = FilenameUtils.getExtension(originalFilename);
				// 文件重新命名
				String newFileName = DateUtils.getDate("yyyyMMddHHmmssSSS") + "." + extName;
				// 上传
				InputStream is = multipartFile.getInputStream();
				String filePath = OssUploadFile.uploadFileToAliyun(is, newFileName, multipartFile.getSize());
				
				JSONObject result = new JSONObject();
				result.put("url", PublicConfig.IMG_ROOT_URL + filePath);
				result.put("state", "SUCCESS");
				result.put("type", extName);
				out.print(result.toJSONString());
			}
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
		
	}

}
